package com.cd.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cd.util.CheckLogin;
import com.cd.util.DBUtil;
import com.cd.util.ReturnResult;

public class LoginServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		ResultSet rs = null;
		PreparedStatement psm = null;;
		String username = "";
		String password = "";
		String result = null;
		if (request.getParameter("username") != null) {
			username = request.getParameter("username");
		}
		if (request.getParameter("password") != null) {
			password = request.getParameter("password");
		}
		CheckLogin checkLogin = new CheckLogin();
		ReturnResult returnResult = checkLogin.checkLogin(username, password);
		switch (returnResult.getCheck())
		{
		case 0:
			session.setAttribute("result", returnResult.getStr());
			response.sendRedirect("errorLogin.jsp");
			break;
		case 1:
			session.setAttribute("loginFlag", 1);
			session.setAttribute("username", username);
			response.sendRedirect("successLogin.jsp");

		default:
			break;
		}
		/*DBUtil database = new DBUtil();
		Connection con = database.getConnection();
		try {
			psm = con.prepareStatement("SELECT username,password from user where username = ?");
			psm.setString(1, username);
			rs = psm.executeQuery();
			if(!rs.next()){
				session.setAttribute("result", "用户名或密码错误");
				response.sendRedirect("errorLogin.jsp");
			}else{
				if(!rs.getString(2).equals(password)){
					session.setAttribute("result", "登录密码错误");
					response.sendRedirect("errorLogin.jsp");
				}else{
					session.setAttribute("loginFlag", 1);
					session.setAttribute("username", username);
					response.sendRedirect("successLogin.jsp");
				}
			}
			
			if(psm.isClosed()==false){
				psm.close();
			}
			if(rs.isClosed()==false){
				rs.close();
			}
			database.closeService();
		} catch (SQLException e) {
			e.printStackTrace();
		}*/
		return;
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
